package com.zcxk.dao.mapper;

import com.zcxk.dao.JdbcPool;
import com.zcxk.dao.dataobject.User;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;

/**
 * @author wangping
 * @date 2023-07-28 16:14
 */
public class UserMapperVert {

  private static final UserMapperVert MAPPER = new UserMapperVert();

  private UserMapperVert() {
    throw new RuntimeException();
  }

  public static UserMapperVert getMapper() {
    return MAPPER;
  }

  public User selectByUserNameAndPwd(String username, String password) {
    String sql = "select * from user where username = ? and password = ?";

    JdbcPool.getPool().getConnection().compose(conn -> {
      // 所有操作都在同一连接上执行
      return conn
        .preparedQuery(sql)
        .execute(Tuple.of(username, password));
    }).onComplete(ar -> {
      if (ar.succeeded()) {
        System.out.println("Done");
        RowSet<Row> rows = ar.result();
        if (rows.rowCount() > 0) {
          for (Row row : ar.result()) {
            JsonObject jsonObject = row.toJson();
          }
        }
      } else {
        System.out.println("Something went wrong " + ar.cause().getMessage());
      }
    });
    return new User();
  }
}
